<?php

namespace App\Game\Logic;

use App\Game\Conf\GameProgress;
use App\Game\Conf\MainCmd;
use App\Game\Conf\SubCmd;
use App\Game\Core\AStrategy;
use App\Game\Core\Message;
use App\Game\Core\Packet;
use App\Game\Core\Robot;
use App\Game\Core\Room;
use App\Game\Timer\GameTimer;
use App\Task\GameTimerTask;
use App\Task\PlayCardTask;
use App\Task\GameSyncTask;

/**
 *  换牌响应
 */
class ChangeCard extends AStrategy
{
    /**
     * 游戏换牌
     */
    public function exec()
    {
        $game_conf = config('game');
        $account = $this->_params['userinfo']['account'];
        $user_room_map_key = sprintf($game_conf['user_room'], $account);
        $room_no = redis()->get($user_room_map_key);
        $change_cards = isset($this->_params['data']['change_cards']) ?
            $this->_params['data']['change_cards'] : null;

        if (!checkGameProgress($room_no, GameProgress::GAME_CHANGE_CARD)) {
            $msg = array('status' => 1, 'info' => '换牌非法, 还未到换牌阶段！');
            Message::pushDataToUser($account, $msg, SubCmd::SUB_GAME_CHANG_CARD_RESP, 0);
            return;
        }
        //检查操作是否已经超时
        if (!checkCounter($room_no)) {
            $msg = ['status' => 1, 'info' => '该操作已超时，系统已自动为您执行默认操作'];
            Message::pushDataToUser($account, $msg, SubCmd::SUB_GAME_CHANG_CARD_RESP, 0);
            return;
        }
//        $robot_conf = config('robot');
        if (empty($room_no)) {
            $msg = ['status' => 1, 'info' => '房间号不存在'];
            Message::pushDataToUser($account, $msg, SubCmd::SUB_GAME_CHANG_CARD_RESP, 0);
            return;
        }
        if (empty($change_cards)) {
            $msg = ['status' => 2, 'info' => '请选择要换的三张牌'];
            Message::pushDataToUser($account, $msg, SubCmd::SUB_GAME_CHANG_CARD_RESP, 0);
            return;
        }
        if (count($change_cards) != 3) {
            $msg = ['status' => 3, 'info' => '只能换三张牌'];
            Message::pushDataToUser($account, $msg, SubCmd::SUB_GAME_CHANG_CARD_RESP, 0);
            return;
        }
        if (!$this->checkColor($change_cards)) {
            $msg = ['status' => 4, 'info' => '请选择同花色的三张牌'];
            Message::pushDataToUser($account, $msg, SubCmd::SUB_GAME_CHANG_CARD_RESP, 0);
            return;
        }

        $chair_id = findChairId($room_no, $account); // 座位号

//        $msg = ['status' => 0, 'chair_id' => $chair_id];
//        Message::broadcast($room_no, $msg, SubCmd::SUB_GAME_CHANG_CARD_RESP);

        $room_change_key = sprintf($game_conf['user_room_change'], $room_no);
        redis()->hSet($room_change_key, (string)$chair_id, json_encode($change_cards));

        $robots = Robot::getRoomRobot($room_no);
        $robotNum = count($robots);
        $realNum = 4 - $robotNum;
//        $ptype = getGameProgressType($room_no);
//        if ($realNum) { //单机
//            ddTitle('do_change_card 11111111  ');
//            //清除前一个定时器
//            putTimerDel($room_no);
//            GameTimer::do_change_card($room_no);
//            return;
//        }
        $arr = redis()->hGetAll($room_change_key);

        if (count($arr) == $realNum) {
            dd('arr', $arr);
            $changeArr = [];
//            $arr = json_decode($arr, true);
            foreach ($arr as $k => $v) {
                $changeArr[$k] = json_decode($v, true);
            }
            redis()->del($room_change_key);
            dd('changeArr', $changeArr);
            $index = 0;
            foreach ($arr as $k => $v) {
                $index++;
                if ($index == $realNum) {
                    if ($k == $chair_id) {
                        ddTitle('do_change_card 0000000000  ');
                        //清除前一个定时器
                        putTimerDel($room_no);
                        GameTimer::do_change_card($room_no, $changeArr);
                    }
                }
            }
        }
    }

    // 校验换牌牌花色：字，条，筒
    protected function checkColor(array $cards)
    {
        $colors = [];
        foreach ($cards as $k => $v) {
            if (in_array($v, [1, 2, 3, 4, 5, 6, 7, 8, 9])) {
                $colors[] = 1; //万
            }
            if (in_array($v, [11, 12, 13, 14, 15, 16, 17, 18, 19])) {
                $colors[] = 2; //筒
            }
            if (in_array($v, [21, 22, 23, 24, 25, 26, 27, 28, 29])) {
                $colors[] = 3; //条
            }
        }
        $colorNum = array_unique($colors);
        if (count($colorNum) > 2) {
            return false;
        }
        return true;
    }

    protected function resp($msg)
    {
        $fd = $this->_params['userinfo']['fd'];
        $serv = $this->_params['serv'];
        $data = Packet::packFormat('ERROR', 0, $msg);
        $return_data = Packet::packEncode($data, MainCmd::CMD_SYS, SubCmd::SUB_GAME_CHANG_CARD_RESP);
        $serv->push($fd, $return_data, WEBSOCKET_OPCODE_BINARY);
    }

}